//
//  WBHomeVC.m
//  WeatherBar
//
//  Created by Bert on 2024/6/24.
//

#import "WBHomeVC.h"
#import "WBShareManager.h"
#import <ContactsUI/ContactsUI.h>
#import "WBLogin01Model.h"
#import "WBLoc01Model.h"
#import "WBDev01Model.h"
#import "WBReg02Model.h"

@interface WBHomeVC () <CNContactPickerDelegate>
@property (nonatomic ,strong) CNContact *selctContact;

@end

@implementation WBHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //    [self setupUI];
        
    // 注册h5监听方法
    [self registerWebHandler];
    
    [self startRequest];
}

- (void)startRequest
{
    [super startRequest];
    //    NSLog(@"web url = %@",self.url);
    [WBUploadManager wb_uploadType:WBUploadTypeLoc01];
}

#pragma mark - 注册JS监听方法。
- (void)registerWebHandler
{
    ///
    WeakSelf(weakSelf);
    [self.bridge registerHandler:@"ObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
        if (data == nil) {
            return;
        }
        
        NSString *value = data[@"type"];
        NSLog(@"----Type:%@ %@",value, data);
        
        if (beNil(value)) {
            return;
        }
        
        //09273888222
        //登录
        if ([value isEqualToString:@"enterLogin"]) {
            
        }
        //退出登录后删除本地数据
        if ([value isEqualToString:@"relogin"]) {

        }
        
        //H5登录和注册成功存储当前用户信息 本地保存ugid等各种数据
        if ([value isEqualToString:@"h5LoginSuccess"]) {
            WBUserModel *uerModel = [WBUserModel modelWithDictionary:data];
            [[WBUserManager shareManager] updateUserInfo:uerModel];
            NSLog(@"本地保存ugid等各种数据 \n");
            [WBUploadManager wb_uploadType:WBUploadTypeDev01];
            [WBUploadManager wb_uploadType:WBUploadTypeLogin01];
        }
        
        //注册成功上报REG02上报项 上传ACQ04大数据埋点数据
        if ([value isEqualToString:@"regSuccess"]) {
            [WBUploadManager wb_uploadType:WBUploadTypeReg02];
        }
        
        //设置页面的标题显示
        if ([value isEqualToString:@"title"]) {
            weakSelf.topBar.var_titleLabel.text = returnBeNil(data[@"title_txt"]);
            weakSelf.topBar.backgroundColor = [UIColor colorWithHexString:returnBeNil(data[@"title_bg"])];
            //            self.topBar.var_titleLabel.textColor = [UIColor colorWithHexString:returnBeNil(data[@"title_txtcolor"])];
        }
        
        //getUserInfo
        if ([value isEqualToString:@"getUserInfo"]) {
            WBUserModel *model = [WBUserManager shareManager].userModel;
            NSDictionary *dic = [model model_keyValues];
            responseCallback([dic mj_JSONString]);
            NSLog(@"回调:%@ \n",dic);
        }
        
        //获取联系人权限
        if ([value isEqualToString:@"getPermissionContact"]) {
            NSDictionary *data = @{@"type": @"getPermissionContact",
                                   @"state" : @"-1"
            };
            [weakSelf.bridge callHandler:@"JavascriptHandler" data:[data mj_JSONString]];
            NSLog(@"异步回调getPermissionContact");
        }
        
        //获取联系人
        if ([value isEqualToString:@"getContacts"] || [value isEqualToString:@"contacts"]) {
            CNContactPickerViewController * pickerVC = [[CNContactPickerViewController alloc]init];
            pickerVC.delegate = weakSelf; //<CNContactPickerDelegate>
            [weakSelf presentViewController:pickerVC animated:YES completion:nil];
            NSLog(@"回调");
        }
        
        //主要在获取紧急联系人时使用，回传给前端联系人信息
        if ([value isEqualToString:@"openTempContacts"]) {
            CNContactPickerViewController * pickerVC = [[CNContactPickerViewController alloc]init];
            pickerVC.delegate = weakSelf; //<CNContactPickerDelegate>
            [weakSelf presentViewController:pickerVC animated:YES completion:nil];
            NSLog(@"回调");
        }
        
        //上传图片
        if ([value isEqualToString:@"uploadPhoto"]) {
   
        }
        
        //登录绑定手机号
        if ([value isEqualToString:@"encrypt"]) {
            NSDictionary *dic = @{@"type" : @"encrypt",
                                  @"encryptedData" : returnBeNil(data[@"beforeEncryptedData"]),
            };
            responseCallback([dic mj_JSONString]);
            NSLog(@"回调:%@ \n",dic);
        }
        
        //判断同一账号是否在手机上重复登录
        if ([value isEqualToString:@"checkPhoneNumLogged"]) {
            NSDictionary *dic = @{@"phone" : returnBeNil(data[@"phoneNum"]), @"exit" : @"1"};
            responseCallback([dic mj_JSONString]);
            NSLog(@"回调:%@ \n", dic);
        }
        
        //UUID 获取手机设备zuid/ 将手机设备ZUID保存到cookie和localStorage
        if ([value isEqualToString:@"getZuid"]) {
            NSString *zuid = [UIDevice wb_getZuid];
            NSDictionary *dic = @{@"zuid" : zuid};
            responseCallback([dic mj_JSONString]);
            NSLog(@"回调:%@", dic);
        }
        
        //获取渠道号，存储到 localStorage
        if ([value isEqualToString:@"getChannel"]) {
            NSDictionary *dic = @{@"type" : @"transmitChannel",
                                  @"channel" : @""
            };
            responseCallback([dic mj_JSONString]);
            NSLog(@"回调:%@", dic);
        }
        
        //标题栏左上角的返回按钮
        if ([value isEqualToString:@"backEvent"]) {
            //            NSDictionary *dic = @{@"phone" : @"092738882", @"exit" : @"0"};
            //            responseCallback([dic JSONString]);
        }
        
        //身份认证上报 拍照上传oss后传递地址给前端
        if ([value isEqualToString:@"idcard"]) {
            //            NSDictionary *dic = @{@"phone" : @"092738882", @"exit" : @"0"};
            //            responseCallback([dic JSONString]);
        }
        
        //分享 客户端实际提供分享功能 返回af回调的渠道号 openShare
        if ([value isEqualToString:@"share"]) {
            [WBShareManager wb_shareWithType:returnBeNil(data[@"inviteType"]) contentUrl:returnBeNil(data[@"contentUrl"])];
        }
        
        //从复制版获取数据
        if ([value isEqualToString:@"bombTxt"]) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = @"";
        }
        
        
        //跳转到其他app或者跳转到appstore
        if ([value isEqualToString:@"wakeUpAppByPackageName"]) {
            NSLog(@"回调");
        }
        
        //上传图片
        if ([value isEqualToString:@"uploadPhoto"]) {
            NSLog(@"回调");
        }
        
        //上报埋点大数据 仅通过firebase上报
        if ([value isEqualToString:@"analyticsEvent"]) {
            NSLog(@"回调:%@ \n", @"");
        }
        
        //上报定位信息 仅通过kibana上报
        if ([value isEqualToString:@"location"]) {
            NSLog(@"回调:");
        }
        
        //上报DEV01信息 仅通过kibana上报
        if ([value isEqualToString:@"DEV01"]) {
            NSLog(@"回调");
        }
        
        //客户端缓存前端需要保存的数据
        if ([value isEqualToString:@"setStorage"]) {
            NSLog(@"回调");
        }
        
        //获取前端缓存在客户端的数据
        if ([value isEqualToString:@"getStorage"]) {
            NSLog(@"回调");
        }
        
        //打电话 唤醒手机自带电话
        if ([value isEqualToString:@"makecall"]) {
            NSString *phone = @"400-895-8818";
            NSMutableString * aStr=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:aStr] options:@{} completionHandler:nil];
            NSLog(@"唤醒手机自带电话");
        }
        
        //新增日程事件 向日历中增加一个日程事件提醒
        if ([value isEqualToString:@"calendarEventAdd"]) {
            NSLog(@"回调");
        }
        
        //删除日程 删除日历中制定的日程事件提醒
        if ([value isEqualToString:@"calendarEventDelete"]) {
            NSLog(@"回调");
        }
        
        //是否开启定位权限
        if ([value isEqualToString:@"locationAgreeOrNot"]) {
            NSLog(@"回调");
        }
        
        //开始定位权限
        if ([value isEqualToString:@"judgePermissionLoc"]) {
            NSLog(@"回调");
        }
        
        //提交申请时最后判断是否开启定位权限 取消或者进入配置都不返回数据，如果是开始权限状态返回数据
        if ([value isEqualToString:@"openDetailAuth"]) {
            NSLog(@"回调");
        }
    }];
    
    
}


#pragma mark - CNContactPickerDelegate

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
    
}

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    self.selctContact = contact;
    
    NSString *name = [NSString stringWithFormat:@"%@ %@", contact.familyName, contact.givenName];
    CNLabeledValue *labeledValue = contact.phoneNumbers.firstObject;
    CNPhoneNumber * phoneValue = labeledValue.value;
    NSString * phoneNumber = phoneValue.stringValue;
    NSLog(@"phoneNumber=%@",phoneNumber);
    
    NSDictionary *statisticsDic = @{
        @"name" : name,
        @"state" : @"1",
        @"mobile" : returnBeNil(phoneNumber)
    };
    NSDictionary *data = @{@"type": @"getContacts",
                           @"allContacts" : @"",
                           @"statistics" : @[statisticsDic]
    };
    [self.bridge callHandler:@"JavascriptHandler" data:[data JSONString]];
}




@end
